CREATE PROCEDURE [dbo].[autoComplete_get_authors]
    @searchPattern nvarchar(256) = null
AS
if(@searchPattern is not null)
BEGIN
    SELECT DISTINCT TOP(20) 
        [dbo].[Authors].[AuthorId],
        [dbo].[Authors].[LastName] + ' ' + [dbo].[Authors].[FirstName]
    FROM [dbo].[Authors]
    WHERE [dbo].[Authors].[FirstName] LIKE '%' + @searchPattern + '%' OR
          [dbo].[Authors].[LastName] LIKE '%' + @searchPattern + '%'
END else
BEGIN
     SELECT TOP(20) 
        [dbo].[Authors].[AuthorId],
        [dbo].[Authors].[LastName] + ' ' + [dbo].[Authors].[FirstName]
    FROM [dbo].[Authors]
    ORDER BY [Authors].[LastName]
END

RETURN 0
